package com.guguju.ssms.feature.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.utils.SPUtils;
import com.blankj.utilcode.utils.ToastUtils;
import com.google.gson.JsonObject;
import com.guguju.ssms.Guguju;
import com.guguju.ssms.R;
import com.guguju.ssms.bean.LoginData;
import com.guguju.ssms.constant.IntentParms;
import com.guguju.ssms.constant.SPConstant;
import com.guguju.ssms.constant.UrlConstant;
import com.guguju.ssms.core.base.BaseActivity;
import com.guguju.ssms.core.http.BaseResponse;
import com.guguju.ssms.core.http.HttpLoader;
import com.guguju.ssms.core.http.JsonCallback;
import com.guguju.ssms.core.log.LogUtil;
import com.guguju.ssms.feature.Home.HomeActivity;
import com.guguju.ssms.utils.LoginUtil;

import okhttp3.Call;
import okhttp3.Response;

public class LoginActivity extends BaseActivity implements LoginContract.View {

    EditText loginNameET;
    EditText passWordET;
    Button button_loginBtn;

    LinearLayout ll_progress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginNameET=(EditText)findViewById(R.id.et_username);
        passWordET=(EditText)findViewById(R.id.et_password);
        button_loginBtn=(Button)findViewById(R.id.button_login);
        ll_progress=(LinearLayout)findViewById(R.id.ll_progress);
        button_loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(loginNameET.getText().toString().equals(""))
                {
                    ToastUtils.showShortToast(LoginActivity.this, "请输入账号",Toast.LENGTH_LONG);
                    return;
                }
                if(passWordET.getText().toString().equals(""))
                {
                    ToastUtils.showShortToast(LoginActivity.this, "请输入密码",Toast.LENGTH_LONG);
                    return;
                }

                UserDataLogin(loginNameET.getText().toString(),passWordET.getText().toString());


            }
        });


    }


    /**
     * 获取用户数据
     */
    private void UserDataLogin(String userName,String password) {
        JsonObject params = new JsonObject();
        params.addProperty("loginName", userName);
        params.addProperty("loginPwd", password);
        //ll_progress.setVisibility(View.VISIBLE);
        HttpLoader.post(UrlConstant.USER_LOGIN, params, true, new JsonCallback<BaseResponse<LoginData>>() {

            @Override
            public void onSuccess(BaseResponse<LoginData> loginData, Call call, Response response) {
                LogUtil.e(UrlConstant.GET_USERINFO + "response;" + response);
                //ll_progress.setVisibility(View.GONE);
                if (loginData.code == 200) {

                    // setUserData(userInfo);
                    Guguju.ASESSTOKEN = loginData.data.getAsessToken();
                    Guguju.USERID = loginData.data.getUserId();
                    Guguju.AUTH = loginData.data.getAuth();
                     loginSuccess(loginData.data);
                    //ToastUtils.showShortToast(LoginActivity.this,loginData.msg ,Toast.LENGTH_LONG);
                }
                else
                {
                    ToastUtils.showShortToast(LoginActivity.this,loginData.msg ,Toast.LENGTH_LONG);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                //ll_progress.setVisibility(View.GONE);
                LogUtil.e(UrlConstant.GET_USERINFO + "response;" + response.body().toString());
                super.onError(call, response, e);
                ToastUtils.showShortToast(LoginActivity.this, e.getMessage());
            }
        });
    }

    @Override
    public void loginSuccess(LoginData loginData) {

        LoginUtil.loginSuccess(this, loginData);
        Intent intent = new Intent(this, HomeActivity.class);
        intent.putExtra(IntentParms.LOGIN_FROM, HomeActivity.FRAGMENT_INDEX);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }


    private long firstTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒，则不退出
                ToastUtils.showShortToast(this, "再按一次退出");
                firstTime = secondTime;//更新firstTime
                return true;
            } else { //两次按键小于2秒时，返回到桌面
                Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
            }
        }
        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }

    @Override
    protected void onPause() {
        Guguju.isOpenLoginActivity = false;
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Guguju.isOpenLoginActivity = false;
        super.onDestroy();
    }

}